package cn.syncrond.tcm.support;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;

public class JettyServer {
	
	private Server server;
	
	public JettyServer(){
		this(System.getProperty("tcm.jetty.host", "0.0.0.0"), Integer.getInteger("tcm.jetty.port", 8080));
	}
	
	public JettyServer(String host, int port){
		this.server = new Server();
		SelectChannelConnector connector = new SelectChannelConnector();
		connector.setHost(host);
		connector.setPort(port);
		this.server.addConnector(connector);
		
		Context context = new Context(server, "/");
		this.server.setHandler(context);
		
		context.addServlet(new ServletHolder(new TcmServlet()), "/tcm/*");
		
	}
	
	/**
	 * 启动服务器
	 */
	public void start(){
		try {
			this.server.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 关闭服务器
	 */
	public void shutdown(){
		try {
			this.server.stop();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args){
		new JettyServer().start();
	}
}
